<?php

namespace Admin\Model;

use Think\Model;

class CategoryModel extends Model {

	/**
	 * 保存数据:当$id为空时添加数据;不为空则更新数据
	 *
	 * @param array $data
	 * @param string $id
	 */
	public function saveData($data, $id = '') {
		$id = intval ( $id );
		$this->error = false;
		// 新增
		if (! $data ['name']) {
			$this->error = '标题不能空';
		} elseif (! eregi ( "^[a-z]+[a-z0-9]+$", $data ['url'] )) {
			$this->error = 'url必须是字母开头,后面只能跟字母和数字';
		}
		if ($id) {
			// 编辑
			if ($data ['name']) {
				$_data ['name'] = $data ['name'];
			}
			if ($data ['url']) {
				$_data ['url'] = $data ['url'];
			}
			if (! $data ['thumb']) {
				$_data ['thumb'] = $data ['thumb'];
			}
			$typeArr = array (
					'article',
					'site'
			);
			if (in_array ( $typeArr, $data ['type'] )) {
				$_data ['type'] = $data ['type'];
			}
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$rs = $this->where ( 'id=' . $id )->save ( $_data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '保存成功';
				} else {
					$result ['status'] = 0;
					$result ['info'] = '没有更新数据';
				}
			}
		} else {
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$rs = $this->add ( $data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '添加成功';
					$result ['id'] = $rs;
				} else {
					$result ['status'] = 0;
					$result ['info'] = '添加失败';
				}
			}
		}
		return $result;
	}

	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, unknown, mixed, string>
	 */
	public function getInfo($id) {
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}
		$result = $this->where ( $where )->find ();
		return $result;
	}
	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 12) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 检查url是否存在
	 *
	 * @param unknown $url
	 * @param string $id
	 *        	传入自己id可以不验证
	 * @return unknown
	 */
	public function checkUrl($url, $id = '') {
		$where = 'url="' . $url . '"';
		if ($id) {
			$where .= ' and id!=' . intval ( $id );
		}
		$rs = $this->where ( $where )->count ();
		return $rs;
	}

	/**
	 * 删除操作(non-PHPdoc)
	 *
	 * @see \Think\Model::delete()
	 */
	public function delete($id) {
		$result ['msg'] = '删除失败';
		$result ['status'] = 0;
		if ($id) {
			// 检查是否有子信息

			$result ['msg'] = '删除成功';
			$result ['status'] = 1;
		} else {
			$result ['msg'] = '删除失败';
			$result ['status'] = 0;
		}
		return $result;
	}
}